﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace Fa.Kanbox.API.Demo
{
    public partial class FMain : Form
    {
        public FMain()
        {
            InitializeComponent();
        }
        private void RefreshList()
        {
            var accountInfo = MyGlobal.ApiClient.GetAccountInfo();
            tsslEmail.Text = accountInfo.Email;
            tsbrSize.Value = (int)((double)accountInfo.SpaceUsed / (double)accountInfo.SpaceQuota * 100);
            var listInfo = MyGlobal.ApiClient.GetListInfo(tbxPath.Text);
            listView1.Items.Clear();
            foreach (var info in listInfo.Contents.OrderByDescending(m => m.IsFolder))
            {
                var item = listView1.Items.Add(new ListViewItem(Path.GetFileName(info.FullPath))
                {
                    Tag = info
                });
                item.ImageKey = info.IsFolder ? "Folder.Png" : "File.Png";
                item.SubItems.Add(info.ModificationDate.ToString("yyyy-MM-dd HH:mm:ss"));
                item.SubItems.Add(info.IsFolder ? "" : info.FileSize.ToString());
                item.SubItems.Add(info.IsShared ? "是" : "否");
            }
            //bsList.DataSource = listInfo.Contents.OrderByDescending(m => m.IsFolder);
        }
        private void FMain_Shown(object sender, EventArgs e)
        {
            tbxPath.Text = "/";
            this.RefreshList();
        }

        private void FMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void tsbRefresh_Click(object sender, EventArgs e)
        {
            this.RefreshList();
        }

        private void bunGoTo_Click(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void bunUp_Click(object sender, EventArgs e)
        {
            var path = tbxPath.Text;
            if (path != "\\")
            {
                tbxPath.Text = System.IO.Path.GetDirectoryName(path);
                this.RefreshList();
            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var item = listView1.GetItemAt(e.X, e.Y);
            if (item == null)
                return;
            var info = item.Tag as KbListContentInfo;
            if (info.IsFolder)
            {
                tbxPath.Text = info.FullPath;
                this.RefreshList();
            }
            else
            {
                var fileName = Path.GetFileName(info.FullPath);
                using (var dialog = new SaveFileDialog()
                {
                    Title = "保存文件",
                    FileName = fileName
                })
                {
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        if (splitContainer1.Panel2Collapsed)
                        {
                            splitContainer1.Panel2Collapsed = false;
                        }
                        var pr = new UcProgress();
                        flowLayoutPanel1.Controls.Add(pr);
                        flowLayoutPanel1.ScrollControlIntoView(pr);
                        pr.DownloadFile(info, dialog.FileName);
                    }
                }
            }

        }

        private void tsbUpdata_Click(object sender, EventArgs e)
        {
            using (var dialog = new OpenFileDialog()
            {
                Title = "选择文件"
            })
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    if (splitContainer1.Panel2Collapsed)
                    {
                        splitContainer1.Panel2Collapsed = false;
                    }
                    var pr = new UcProgress();
                    flowLayoutPanel1.Controls.Add(pr);
                    flowLayoutPanel1.ScrollControlIntoView(pr);
                    var fileName = Path.GetFileName(dialog.FileName);
                    pr.UploadFile(Path.Combine(tbxPath.Text, fileName), dialog.FileName, (args) =>
                    {
                        if (!args.Cancelled && args.Error == null)
                        {
                            this.RefreshList();
                        }
                    });
                }
            }
        }

        private void tsbCreateFolder_Click(object sender, EventArgs e)
        {
            using (var dialog = new FCreateFolderDialog() { Text = "新建文件夹" })
            {
               if (dialog.ShowDialog() == DialogResult.OK)
                {
                    var path=tbxPath.Text + "/" + dialog.FolderName;
                    MyGlobal.ApiClient.CreateFolder(path);
                    this.RefreshList();
                }
            }
            
        }

        private void tsmiShare_Click(object sender, EventArgs e)
        {
            var info = listView1.SelectedItems[0].Tag as KbListContentInfo;
            using (var dialog = new FShareDialog() { Text = "创建共享" })
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    MyGlobal.ApiClient.Share(info.FullPath, dialog.Users);
                    this.RefreshList();
                }

            }
        }

        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定要删除所选文件或文件夹吗？", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                foreach (ListViewItem item in listView1.SelectedItems)
                {
                    var info = item.Tag as KbListContentInfo;
                    MyGlobal.ApiClient.Delete(info.FullPath);
                }
                this.RefreshList();
            }
        }

        private void tsmiMove_Click(object sender, EventArgs e)
        {
            var info = listView1.SelectedItems[0].Tag as KbListContentInfo;
            using (var dialog = new FSelectFolderDialog() { Text = "选择目标文件夹" })
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    var dPath = dialog.FolderInfo.FullPath + "/" + Path.GetFileName(info.FullPath);
                    if (Path.GetDirectoryName(info.FullPath) == Path.GetDirectoryName(dPath))
                    {
                        MessageBox.Show("所选文件夹，不能与目标文件夹相同");
                        return;
                    }
                    MyGlobal.ApiClient.Move(info.FullPath, dPath);
                    this.RefreshList();
                }

            }
        }

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && listView1.SelectedItems.Count>0)
            {
                contextMenuStrip1.Show(listView1, e.Location);
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            var selectItem = listView1.SelectedItems[0];
            if ((selectItem.Tag as KbListContentInfo).IsFolder)
            {
                tsmiShare.Visible = true;
            }
            else {
                tsmiShare.Visible = false;
            }
        }

        private void tsbPendingShares_Click(object sender, EventArgs e)
        {
            using (var dialog = new FPendingShares() { Text = "选择目标文件夹" })
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    //var dPath = dialog.FolderInfo.FullPath + "/" + Path.GetFileName(info.FullPath);
                    //if (Path.GetDirectoryName(info.FullPath) == Path.GetDirectoryName(dPath))
                    //{
                    //    MessageBox.Show("所选文件夹，不能与目标文件夹相同");
                    //    return;
                    //}
                    //MyGlobal.ApiClient.Move(info.FullPath, dPath);
                    this.RefreshList();
                }

            }
            
        }
    }
}
